package com.xiaozhu.ai_config;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author 朱慧军
 * @version 1.0
 */
@Configuration
@EnableCaching
public class RedisController {

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){

        // 1: 创建对象
        RedisTemplate<Object, Object> template = new RedisTemplate<>();

        // 2: 讲对象加入到工厂中
        template.setConnectionFactory(redisConnectionFactory);

        // 序列化key
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());

        // value序列化
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        //让 RedisTemplate 完成它内部必要的初始化操作。
        template.afterPropertiesSet();

        return template;

    }

}
